package validator.util;

import java.lang.annotation.Annotation;
import java.util.HashMap;

import validator.annotation.Handle;
import validator.handle.Validator;

class AnnotationFactory {

	private static final HashMap<Class<? extends Annotation>, Validator> map = new HashMap<Class<? extends Annotation>, Validator>();

	private AnnotationFactory() {
	}

	protected static Validator getValidator(Class<? extends Annotation> clazz) {
		if (map.containsKey(clazz))
			return map.get(clazz);

		if (!clazz.isAnnotationPresent(Handle.class))
			return null;

		try {
			Validator validator = clazz.getAnnotation(Handle.class).value().newInstance();
			map.put(clazz, validator);
			return validator;
		} catch (Exception e) {
			return null;
		}
	}
}
